package com.Controller.Login;


import com.Common.CodeMsg;
import com.HandleExceptioin.ExceptionEnum;
import com.Mapper.Admin.MenuMapper;
import com.Model.Admin.Menu;

import java.util.List;

import com.Model.Admin.UserInfo;
import com.Service.Admin.UserInfoService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.crypto.hash.Md5Hash;

import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 * @Author: suzr
 * @Date: 2019-7-10 18:02
 * @return:
 * @throws:
 */
@Controller
public class LoginController {
    @Autowired
    private Environment env;


    @Autowired
    private MenuMapper menuMappers;
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping(value = {"/login", "unauth"})
    public String index() {

        return "/login";
    }

    @RequestMapping(value = {"/", "unauth"})
    public String index0() {

        return "/login";
    }

    @RequestMapping(value = {"/doLogin","unauth"}, method = RequestMethod.POST)
    public ModelAndView login(HttpServletRequest request, @RequestParam String userName, @RequestParam String password) {

/*
        String userName=userInfo.getUserName();
        String password=userInfo.getPassword();*/
        ModelAndView m = new ModelAndView();
        String newpassword = "";
        String errorMsg = "";
        if(userName==null||userName.isEmpty())
        {
            m.addObject("userName", CodeMsg.userName_is_empty.getName());
            m.setViewName("login");
            return  m;
        }
        //1.获取Subject实例对象
        Subject subject = SecurityUtils.getSubject();

        try {
            //2、判断当前用户是否登陆

           // if (subject.isAuthenticated()) {
                String newPsd = new Md5Hash(password, env.getProperty("shiro.encrypt.password.salt")).toString();
                newpassword = newPsd;
                //3、将用户名和密码封装到 UsernamePasswordToken
                UsernamePasswordToken token = new UsernamePasswordToken(userName, newPsd);
                //4、认证
                subject.login(token);
                List<Menu> lst = menuMappers.getMenuByUser(userName);
                UserInfo userInfo = userInfoService.getUserInfo(userName, newpassword);

                Session session = subject.getSession();
                //保存用户基本信息
                userInfo.setMenuList(lst);
                session.setAttribute("user", userInfo);

                m.setViewName("index");
                return m;
           // }
        } catch (UnknownAccountException e) {
            errorMsg = e.getMessage();
            m.addObject("userName", CodeMsg.user_is_exist.getName());
            m.setViewName("login");
            return  m;
        } catch (DisabledAccountException e) {
            errorMsg = e.getMessage();
            m.addObject("userName", userName);
            m.setViewName("login");
            return  m;
        } catch (IncorrectCredentialsException e) {
            errorMsg = e.getMessage();
            m.addObject("userName", CodeMsg.pwd_is_wrong.getName());
            m.setViewName("login");
            return  m;
        }
        catch (AuthenticationException e)
        {
            m.addObject("userName", CodeMsg.authentication_is_fail.getName());
            m.setViewName("login");
            //return  m;
        }
        catch (Exception e) {
            errorMsg = ExceptionEnum.LOGINERR.getMessage();
            e.printStackTrace();
            m.setViewName("login");
            return  m;
        }

        m.setViewName("index");
        return m;
    }

    /**
     * 退出登陆
     *
     * @return
     */
    @RequestMapping(value = "/logout")
    public String logout() {
        SecurityUtils.getSubject().logout();
        return "/login";
    }

}
